package com.agree;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Arrays;

public class APP {

	
	public static void server() {
		Socket sk = new Socket();
		SocketAddress socketAddress = new InetSocketAddress("127.0.0.1",1717);
		try {
			sk.connect(socketAddress);
			
			InputStream in = sk.getInputStream();
			byte[] b = new byte[1024];
			int len = -1;
			//StringBuffer sb = new StringBuffer();
			while((len = in.read(b))!=-1) {
				String str = new String(b,0,len);
			//	sb.append(str);
				System.out.println(str);
				break;
			}
		//	System.out.println(sb.toString());
		} catch (IOException e) {
		
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		//while(true) {
		//	server();
		//}
		
		byte[] b = intToByteArray(22636);
		
		show(b);
		
		byte[] m = {0,0,4, 56};
		int res = byteArrayToInt(m);
		
		System.out.println(res);
		
	}
	
	public static void show(byte[] b) {
		System.out.println(b[3]);
		System.out.println(b[2]);
		System.out.println(b[1]);
		System.out.println(b[0]);
	}
	
	public static byte[] intToByteArray(int i) {   
	    byte[] result = new byte[4];   
	    // 由高位到低位   
	    result[0] = (byte) ((i >> 24) & 0xFF);   
	    result[1] = (byte) ((i >> 16) & 0xFF);   
	    result[2] = (byte) ((i >> 8) & 0xFF);   
	    result[3] = (byte) (i & 0xFF);   
	    return result;   
	}   
	
	public static int byteArrayToInt(byte[] bytes) {   
	    int value = 0;   
	    // 由高位到低位   
	    for (int i = 0; i < 4; i++) {   
	        int shift = (4 - 1 - i) * 8;   
	        value += (bytes[i] & 0x000000FF) << shift;// 往高位游   
	    }   
	    return value;   
	}   
}
